# AlbumScreen - This screen allows user to browse and play tracks of the album
# Copyright (C) 2007 Lauri Taimila
# 
# Entertainer is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Entertainer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import clutter

from frontend.user_event import UserEvent
from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

# Widgets
from frontend.gui.widgets.text_menu import TextMenu
from frontend.gui.widgets.text_menu_item import TextMenuItem
from frontend.gui.widgets.list_indicator import ListIndicator
from frontend.gui.widgets.eyecandy_texture import EyeCandyTexture

# Music library
from frontend.medialibrary.music import MusicLibrary
from frontend.medialibrary.music import Track
from frontend.medialibrary.music import Album

class AlbumScreen(Screen):
    """
    AlbumScreen
    
    Screen that allows user to browse and play tracks of the music album.
    """
    
    def __init__(self, ui, frontend, album):
        """
        Initialize screen
        
        @param ui: UserInterface object
        @param frontend: FrontendClient object
        @param album: Album object (We display information of this album)
        """
        self.group = clutter.Group()
        Screen.__init__(self, ui.STAGE_WIDTH, ui.STAGE_HEIGHT)
        
        self.ui = ui
        self.frontend = frontend
        self.player = self.ui.get_media_player()
        self.theme = frontend.getTheme()
        self.library = self.frontend.get_music_library()
        self.album = album
        
        # Screen Title (Displayed at the bottom left corner)
        screen_title = clutter.Label()
        screen_title.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.13)) + "px")
        screen_title.set_color(self.theme.getColor("screentitle"))
        screen_title.set_text(self.album.get_artist())
        screen_title.set_name("screen_title")
        screen_title.set_position(self.ui.x(0), self.ui.y(0.87))
        screen_title.show()
        self.group.add(screen_title)
        
        # Create and initialize screen items
        self._create_track_menu()
        self._create_album_cover_texture()
        self._create_album_info()
        
        #List indicator
        self.li = ListIndicator(0.2, 0.045, self.theme, ListIndicator.VERTICAL)
        self.li.set_position(self.ui.STAGE_WIDTH - self.li.get_width() - self.ui.x(0.066), 
                             self.ui.STAGE_HEIGHT - self.ui.y(0.11))
        self.li.set_delimiter(" | ")
        self.li.show()
        self.li.set_maximum(self.track_menu.get_number_of_items())
        self.li.set_current(1)
        self.group.add(self.li)
        
        self.group.show()
        ui.getStage().add(self.group)

    def _create_album_cover_texture(self):
        """
        Create a texture that is displayed next to track list. This texture
        displays album cover art.
        """
        if(self.album.has_album_art()):
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.album.get_album_art_url())
        else:
            pixbuf = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("default_album_art"))
        self.art = EyeCandyTexture(pixbuf, self.ui.x(0.3148),self.ui.y(0.5599))
        self.art.set_position(self.ui.x(0.1),self.ui.y(0.13))
        self.art.show()
        self.art.set_rotation(clutter.Y_AXIS, 25, 0, 0, 0)
        self.group.add(self.art)

    def _create_album_info(self):
        """
        Create album info labels.
        """
        album = clutter.Label()
        album.set_font_name(self.theme.getFont() + " bold " + str(self.ui.y(0.04167)) + "px")
        album.set_color(self.theme.getColor("text"))
        if self.album.get_year() != 0:
            album.set_text(self.album.get_title() + ", " + str(self.album.get_year()))
        else:
            album.set_text(self.album.get_title())
        album.set_position(self.ui.x(0.5015), self.ui.y(0.13))
        album.show()
        self.group.add(album)
        
        num_of_tracks = clutter.Label()
        num_of_tracks.set_font_name(self.theme.getFont() + " bold " + str(self.ui.y(0.02604)) + "px")
        num_of_tracks.set_color(self.theme.getColor("subtitle"))
        length = str(self.album.get_total_length() / 60)
        num_of_tracks.set_text(str(self.album.get_number_of_tracks()) + " tracks, " + length + " minutes")
        num_of_tracks.set_size(self.ui.x(0.4026), self.ui.y(0.0651))
        num_of_tracks.set_position(self.ui.x(0.5015), self.ui.y(0.18))
        num_of_tracks.show()
        self.group.add(num_of_tracks)
        

    def _create_track_menu(self):
        """
        Create track menu. This menu contains list of all tracks on album.
        """
        self.track_menu = TextMenu(self.theme, 
                               self.frontend.get_configuration().show_effects())
        self.track_menu.set_row_count(1)
        self.track_menu.set_visible_column_count(7)
        self.track_menu.set_item_size(self.ui.x(0.4393), self.ui.y(0.0781))
        self.track_menu.set_position(self.ui.x(0.4978), self.ui.y(0.2344))
        
        # Create menu items based on MusicLibrary
        tracks = self.album.get_tracks()
        for track in tracks:
            track_length = str(track.get_length() / 60) + ":" + str(track.get_length() % 60).zfill(2)
            item = TextMenuItem(self.ui.x(0.4393), self.ui.y(0.0781), self.theme, track.get_title(), track_length)
            item.set_userdata(track)
            self.track_menu.add_actor(item)
                
        self.track_menu.show()
        self.track_menu.set_active(True)
        self.group.add(self.track_menu)
        
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.NORMAL
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "AlbumScreen"
    
    def is_interested_in_play_action(self):
        """
        Override function from Screen class. See Screen class for 
        better documentation.
        """
        return True
    
    def execute_play_action(self):
        """
        Override function from Screen class. See Screen class for 
        better documentation.
        """
        track = self.track_menu.get_current_menuitem().get_userdata()
        self.player.set_media(track)
        self.player.play()
    
    def handle_user_event(self, event):   
        """ 
        Handle screen specific user events
        @param event: UserEvent object
        """
        type = event.get_type()

        if type == UserEvent.NAVIGATE_UP:
            self.track_menu.move(TextMenu.UP)
            self.li.set_current(self.track_menu.get_current_postition() + 1)
        
        elif type == UserEvent.NAVIGATE_DOWN:
            self.track_menu.move(TextMenu.DOWN)
            self.li.set_current(self.track_menu.get_current_postition() + 1)
        
        elif type == UserEvent.NAVIGATE_LEFT:
            pass
        
        elif type == UserEvent.NAVIGATE_RIGHT:
            pass
        
        elif type == UserEvent.NAVIGATE_SELECT:
            track = self.track_menu.get_current_menuitem().get_userdata()
            self.ui.changeScreen(self.ui.createScreen("audio_play", track), Transition.FORWARD)

